package com.yasaka.stock.common;

import java.io.InputStream;
import java.net.URLEncoder;
import java.text.MessageFormat;

import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

@Component
public class GoogleTranslate {

	protected static final String URL_TEMPLATE = "http://translate.google.cn/?langpair={0}&text={1}";
	protected static final String ID_RESULTBOX = "result_box";
	protected static final String ENCODING = "UTF-8";

	protected static final String AUTO = "auto"; // google自動判斷來源語系
	protected static final String TAIWAN = "zh-TW"; // 繁中
	protected static final String CHINA = "zh-CN"; // 簡中
	protected static final String ENGLISH = "en"; // 英
	protected static final String JAPAN = "ja"; // 日

	/**
	 * <pre>Google翻譯
	 * PS: 交由google自動判斷來源語系
	 * </pre>
	 * 
	 * @param text
	 * @param target_lang 目標語系
	 * @return
	 * @throws Exception
	 */
	
	private static Document translateDoc(final String text, final String src_lang, final String target_lang)
			throws Exception{
		InputStream is = null;
		
		try {
			// create URL string
			String url = MessageFormat.format(URL_TEMPLATE,
					URLEncoder.encode(src_lang + "|" + target_lang, ENCODING),
					URLEncoder.encode(text, ENCODING));

			// connect & download html
			is = HttpClientUtil.downloadAsStream(url);

			// parse html by Jsoup
			return Jsoup.parse(is, ENCODING, "");
			

		} finally {
			IOUtils.closeQuietly(is);
			is = null;
		}
	}
	public static String translate(final String text, final String target_lang) throws Exception {
		return translate(text, AUTO, target_lang);
	}
	
	public static String languages(final String text) throws Exception {
		
		Document doc = translateDoc(text,AUTO,ENGLISH);
		Element ele = null;
		try{
			ele = doc.getElementById("gt-sl");			
			Elements eles = ele.getElementsByAttribute("selected");
			
			for (Element mEle : eles){
				if (!"auto".equals(mEle.attr("value")))
					return mEle.attr("value"); 			
			}			
			return "auto";	
		}finally {
			
			doc = null;
			ele = null;
		}		
	}

	/**
	 * <pre>Google翻譯</pre>
	 * 
	 * @param text
	 * @param src_lang 來源語系
	 * @param target_lang 目標語系
	 * @return
	 * @throws Exception
	 */
	public static String translate(final String text, final String src_lang, final String target_lang) 
			throws Exception{
		Document doc = translateDoc(text,src_lang,target_lang);
		Element ele = null;
		try{
			ele = doc.getElementById(ID_RESULTBOX);
			String result = "";
			
			for (Element mEle : ele.children()){
				for (Node mNode : mEle.childNodes()){
					if (mNode.nodeName().equals("br"))
						result += "\r\n";
					else
					result += mNode.toString();					
				}
//				result += mEle.text();
//				int mBrCount = mEle.getElementsByTag("br").size();				
//				for (int i = 0; i < mBrCount -1 ;i++){				
//					result += "\r\n";
//					}				
			}
			
			
			return result;	
		}finally {
			
			doc = null;
			ele = null;
		}		
	}

}